import org.apache.commons.io.FileUtils;
import java.io.File;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class Show {
    public static TimeUnit time = TimeUnit.SECONDS;
    public static void showAdmin(Scanner input){
        System.out.println("==============欢迎进入管理界面===============");
        System.out.println("1.查看所有账号信息");
        System.out.println("2.删除账户");
        System.out.println("3.添加题目");
        System.out.println("4.退出系统");
        int choose = input.nextInt();
        if(choose==4){
            System.out.println("已退出...");
            return ;
        }
        else if(choose==1){
            showMembers(input);
        }
        else if(choose==2){
            deleteInfo(input);

        }
        else if(choose==3){
            AddProblems(input);
        }
        else{
            System.out.println("当前输入的操作不存在!");
        }

        //停顿1s,再返回菜单
        System.out.println("正在返回...");
        try {
            time.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        showAdmin(input);

    }


    public static void showCommand(Scanner input,String id){

        System.out.println("==============欢迎进入操作界面===============");
        System.out.println("1.开始答题");
        System.out.println("2.学习党史");
        System.out.println("3.回顾错题");
        System.out.println("4.个人信息");
        System.out.println("5.排行榜");
        System.out.println("6.退出系统");
        int choose = input.nextInt();
        if(choose==6){return ;}
        else if(choose==1){
            Exam ex = new Exam(id);
            ex.exam(input);
        }
        else if(choose==2){
            study(input);
        }
        else if(choose==3){
            reviewError(input,id);
        }
        else if(choose==4){

            QueryInfo(input,id);
        }
        else if(choose==5) {
            File dir = new File("users");
            File[] fileList = dir.listFiles();
            ArrayList<Map<String, Object>> accounts = new ArrayList<>();
            for (File f : fileList) {
                accounts.add(FileOpera.JsonRead(f.getName()));
            }
            Collections.sort(accounts, new Comparator<Map<String, Object>>() {    //对accounts进行排序
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    int rank1 = (int) o1.get("正确题数");//name1是从你list里面拿出来的一个
                    int rank2 = (int) o2.get("正确题数"); //name1是从你list里面拿出来的第二个name
                    return rank1 > rank2 ? 1 : 0;
                }
            });

            System.out.println(" ███████████     █████████   ██████   █████ █████   ████\n" +
                    "▒▒███▒▒▒▒▒███   ███▒▒▒▒▒███ ▒▒██████ ▒▒███ ▒▒███   ███▒ \n" +
                    " ▒███    ▒███  ▒███    ▒███  ▒███▒███ ▒███  ▒███  ███   \n" +
                    " ▒██████████   ▒███████████  ▒███▒▒███▒███  ▒███████    \n" +
                    " ▒███▒▒▒▒▒███  ▒███▒▒▒▒▒███  ▒███ ▒▒██████  ▒███▒▒███   \n" +
                    " ▒███    ▒███  ▒███    ▒███  ▒███  ▒▒█████  ▒███ ▒▒███  \n" +
                    " █████   █████ █████   █████ █████  ▒▒█████ █████ ▒▒████\n" +
                    "▒▒▒▒▒   ▒▒▒▒▒ ▒▒▒▒▒   ▒▒▒▒▒ ▒▒▒▒▒    ▒▒▒▒▒ ▒▒▒▒▒   ▒▒▒▒ \n");
            System.out.println("----------------------------------------------");
            System.out.println("| 排名  | 正确题数 | ID     | 个签              ");
            int index=1;
            for (Map account : accounts) {
                System.out.printf("| %-2d   | %-6d  | %-7s| %-10s   \n",index++,account.get("正确题数"),account.get("id"),account.get("motto"));
            }
        }

        System.out.println("正在返回...");
        try {
            time.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        showCommand(input,id);

    }

    public static void study(Scanner input) {
        System.out.println("======开始学习党史   输入exit结束=====");
        int index_max=FileOpera.getFileNums("problems","study");
        int index=0;
        String command = "";
        while(!command.equals("exit"))
        {
            List<String> content = FileOpera.ReadLines("problems", "study", (++index) + "");
            for (String s : content)
            {
                System.out.println(s);
            }
            if(index==index_max)
            {
                System.out.println();
                System.out.println("到底了~~~~");
                break;
            }
            System.out.println("====~===========");

            command = input.next();
        }

        System.out.println("阅读结束!");

        try {
            time.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


    public static void reviewError(Scanner input,String id) {
        System.out.println("======错题集======");
        List<String> content = FileOpera.ReadLines("users",id,"Error_book");
        for(String s:content)
        {
            System.out.println(s);
        }

        System.out.println("===================");
        System.out.println("输入exit退出--------");
        String c = input.next();   //
        try {
            time.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public static void QueryInfo(Scanner input,String id) {
        System.out.println("======个人信息======");
        Map<String,Object> account=FileOpera.JsonRead(id);
        System.out.println("昵称: "+account.get("id"));
        System.out.println("真实姓名: "+account.get("name"));
        System.out.println("年龄: "+account.get("age"));
        System.out.println("个性签名: "+account.get("motto"));
        System.out.println("正确题数: "+account.get("正确题数"));
        System.out.println("===================");
        System.out.println("输入exit退出--------");
        String c = input.next();        //
        try {
            time.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void showMembers(Scanner input) {
        System.out.println("======成员信息======");
        File dir = new File("users");
        File[] fileList = dir.listFiles();
        int UsersNum = fileList.length;
//        System.out.println(UsersNum);
        for(File f :fileList)
        {
            Map<String,Object> account=FileOpera.JsonRead(f.getName());
            if(account==null){     //文件打开错误，退出操作
                System.out.println("文件读取错误！");
                return ;
            }
//            文件正常读取json数据
//            System.out.println("-----------------------");
            System.out.println("账户ID: "+f.getName());
            for (Map.Entry<String,Object> entry :account.entrySet()) {          //遍历读取信息
                System.out.print("" + entry.getKey() + " : " + entry.getValue()+" || ");
            }
            System.out.println();
            System.out.println("---------------------------------------------------------");
        }

        System.out.println("=========共计"+ UsersNum+"个账户==========");
        System.out.println("输入exit退出...");
        String c = input.next();
        try {
            time.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
    public static void AddProblems(Scanner input){
        System.out.println("======题目系统======");
        System.out.println("1.填空题");
        System.out.println("2.选择题");
        int choose = input.nextInt();
        String type = choose==1 ?"tiankong":"xuanze";

        int index_max = FileOpera.getFileNums("problems",type);
        System.out.println("请输入题目内容，最后一行输入答案");
        System.out.println("输入exit结束! ");
        System.out.println("==============");
        System.out.println();
        List<String> proContent = new ArrayList<>();                         //错误写法 List<String> contentAll = new List<String>();     List是一个接口，而ArrayList是List接口的一个实现类。
        String contentLine = input.next();
        while(!contentLine.equals("exit"))
        {
            proContent.add(contentLine);
            contentLine = input.next();
        }
        String path = "problems/"+type+"/"+((++index_max)+"")+".txt";
        try {
            FileUtils.writeLines(new File(path),"UTF-8",proContent,true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("添加题目成功!");
        try {
            time.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public static void deleteInfo(Scanner input){
        System.out.println("======删除账户======");
        System.out.println("输出所要删除的账户ID: ");
        String id = input.next();
        File dir = new File("users/"+id);
        File[] listFiles = dir.listFiles();
        for(File file : listFiles){
            System.out.println("Deleting "+file.getName());
            file.delete();
        }
        System.out.println("成功删除此账户!");

        try {
            time.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
